//
//  MFACommonUtility.m
//  NewMissFresh
//
//  Created by 董宝君 on 2019/6/24.
//  Copyright © 2019 com.missfresh. All rights reserved.
//

#import "MFACommonUtility.h"
#import "MFALogger.h"

@implementation MFACommonUtility

/**
 返回文件下的固定路径
 
 @param name 名称
 @return 文件路径
 */
+ (NSString *)filePathWithName:(NSString *)name {
    //忽略文件后缀
    NSString *filename = [NSString stringWithFormat:@"mfanalytics-%@.plist", name];
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]
                          stringByAppendingPathComponent:filename];
    MFADebug(@"filepath for %@ is %@", name, filepath);
    return filepath;
}

/**
 获取网络状态
 
 @return 网络状态的字符串
 */
+ (MFAnalyticsNetworkType)networkType {
    MFAnalyticsNetworkType type = MFAnalyticsNetworkTypeNONE;
    
    @try {
        MFAReachability *reachability = [MFAReachability reachability];
        MFAReachabilityStatus status = reachability.status;
        
        if(status == MFAReachabilityStatusWiFi){
            type = MFAnalyticsNetworkTypeWIFI;
        }else if (status == MFAReachabilityStatusWWAN){
            MFAReachabilityWWANStatus wwanStatus = reachability.wwanStatus;
            if(wwanStatus == MFAReachabilityWWANStatus2G){
                type = MFAnalyticsNetworkType2G;
            }else if (wwanStatus == MFAReachabilityWWANStatus3G){
                type = MFAnalyticsNetworkType3G;
            }else if (wwanStatus == MFAReachabilityWWANStatus4G){
                type = MFAnalyticsNetworkType4G;
            }else{
                type = MFAnalyticsNetworkTypeNONE;
            }
        }
        
    } @catch (NSException *exception) {
        MFADebug(@"%@: %@",self, exception);
    }
    
    return type;
}


/**
 网络类型名称

 @return 网络类型名称 WIFI、2G、3G、4G、none
 */
+ (NSString *)networkTypeName {
    NSString *name = @"none";
    
    @try {
        MFAReachability *reachability = [MFAReachability reachability];
        MFAReachabilityStatus status = reachability.status;
        
        if(status == MFAReachabilityStatusWiFi){
            name = @"WIFI";
        }else if (status == MFAReachabilityStatusWWAN){
            MFAReachabilityWWANStatus wwanStatus = reachability.wwanStatus;
            if(wwanStatus == MFAReachabilityWWANStatus2G){
                name = @"2G";
            }else if (wwanStatus == MFAReachabilityWWANStatus3G){
                name = @"3G";
            }else if (wwanStatus == MFAReachabilityWWANStatus4G){
                name =@"4G";
            }else{
                name = @"none";
            }
        }
        
    } @catch (NSException *exception) {
        MFADebug(@"%@: %@",self, exception);
    }
    
    return name;
}

@end
